package com.ruoyi.customerSample.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.customerSample.domain.Customer;
import com.ruoyi.customerSample.domain.EntrustedBills;
import com.ruoyi.customerSample.mapper.CustomerMapper;
import com.ruoyi.customerSample.mapper.EntrustedBillsMapper;
import com.ruoyi.customerSample.service.IEntrustedBillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 检测检验委托单据Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-23
 */
@Service
public class EntrustedBillsServiceImpl implements IEntrustedBillsService
{
    @Autowired(required = false)
    private EntrustedBillsMapper entrustedBillsMapper;

    @Autowired(required = false)
    private CustomerMapper customerMapper;

    /**
     * 查询检测检验委托单据
     * 
     * @param id 检测检验委托单据主键
     * @return 检测检验委托单据
     */
    @Override
    public EntrustedBills selectEntrustedBillsById(String id)
    {
        return entrustedBillsMapper.selectEntrustedBillsById(id);
    }

    /**
     * 获取最大检测编号
     * @param testYear
     * @return
     */
    @Override
    public String selectEntrustedNumMax(String testYear) {
        //获取当前最大项目编号
        Integer maxAgreementNum = entrustedBillsMapper.selectEntrustedNumMax(testYear);
        if(maxAgreementNum == null){
            maxAgreementNum = 0;
        }
        return "KY-"  + testYear + "-"  + String.format("%03d", maxAgreementNum + 1);
    }

    /**
     * 查询检测检验委托单据列表
     * 
     * @param entrustedBills 检测检验委托单据
     * @return 检测检验委托单据
     */
    @Override
    public List<EntrustedBills> selectEntrustedBillsList(EntrustedBills entrustedBills)
    {
        return entrustedBillsMapper.selectEntrustedBillsList(entrustedBills);
    }

    /**
     * 新增检测检验委托单据
     * 
     * @param entrustedBills 检测检验委托单据
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertEntrustedBills(EntrustedBills entrustedBills)
    {
        entrustedBills.setId(IdUtils.fastSimpleUUID());
        entrustedBills.setTestYear(DateUtils.getYear());
        int ret = entrustedBillsMapper.insertEntrustedBills(entrustedBills);
        if(ret > 0) {
            //判断当前单位是否存在，不存在添加到单位信息中
            Customer customer = customerMapper.selectCustomerByName(entrustedBills.getClient());
            if(null == customer) {
                customer = new Customer();
                customer.setId(IdUtils.fastSimpleUUID());
                customer.setCompanyName(entrustedBills.getClient());
                customer.setAdress(entrustedBills.getAdress());
                customer.setCharger(entrustedBills.getCharger());
                customer.setChargerPhone(entrustedBills.getChargerPhone());
                customerMapper.insertCustomer(customer);
            }
        }
        return ret;
    }

    /**
     * 修改检测检验委托单据
     * 
     * @param entrustedBills 检测检验委托单据
     * @return 结果
     */
    @Override
    public int updateEntrustedBills(EntrustedBills entrustedBills)
    {
        return entrustedBillsMapper.updateEntrustedBills(entrustedBills);
    }

    /**
     * 批量删除检测检验委托单据
     * 
     * @param ids 需要删除的检测检验委托单据主键
     * @return 结果
     */
    @Override
    public int deleteEntrustedBillsByIds(String[] ids)
    {
        return entrustedBillsMapper.deleteEntrustedBillsByIds(ids);
    }

    /**
     * 删除检测检验委托单据信息
     * 
     * @param id 检测检验委托单据主键
     * @return 结果
     */
    @Override
    public int deleteEntrustedBillsById(String id)
    {
        return entrustedBillsMapper.deleteEntrustedBillsById(id);
    }
}
